package qeb.handheldmedical.patient.merge;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import qeb.handheldmedical.LRLayout;
import qeb.handheldmedical.R;
import qeb.handheldmedical.databinding.ItemMedicineListBinding;


/**
 * Created by Administrator on 2018/4/12.
 */

public class MedicineListAdapter extends RecyclerView.Adapter<MedicineListAdapter.ItemViewHolder>{

    private Context context;
    private LayoutInflater inflater;
    private Object mEventListener;

    private List<MedicineC> totalDates = new ArrayList<>();



    public void setDates(List<MedicineC> models) {
//        totalDates.clear();
        this.totalDates = models;
        notifyDataSetChanged();
    }
    public void addMoreDate(List<MedicineC> searchResponse){
        if(null != this.totalDates){
            this.totalDates.addAll(searchResponse);
            notifyDataSetChanged();
        }
    }

    public void setEventListener(Object mEventListener) {
        this.mEventListener = mEventListener;
    }

    public MedicineListAdapter(Context content) {
        this.context = content;
    }


    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (null == inflater)
            inflater = LayoutInflater.from(parent.getContext());

        return new ItemViewHolder(DataBindingUtil.inflate(inflater, R.layout.item_medicine_list,parent,false));
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        Log.e("PXY", "select  position: "+position+ "  getAdapterPosition :"+holder.getAdapterPosition());
        holder.setIsRecyclable(false);
        holder.bind(holder,mEventListener,totalDates.get(position).getValue().get(0),position);

    }
    @NonNull
    private LRLayout content(Context context, MedicineC.MedicineDetail md)  {
        LRLayout lrLayout = new LRLayout(context);
        if(!TextUtils.isEmpty(md.getYwMc()) && !TextUtils.isEmpty(md.getYwgg())){
            lrLayout.left(md.getYwMc()+"  "+md.getYwgg());
        }
       if(!TextUtils.isEmpty(md.getDose()) && !TextUtils.isEmpty(md.getDose_unit())){
           lrLayout.right(md.getDose() +md.getDose_unit());
       }
        return lrLayout;
    }
    @Override
    public int getItemCount() {
        return null == totalDates ? 0 : totalDates.size();
    }
      class ItemViewHolder extends RecyclerView.ViewHolder{
        private ItemMedicineListBinding binding;
        public ItemViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = (ItemMedicineListBinding) binding;
        }

        public void bind(ItemViewHolder holder,Object mEventHandler, MedicineC.MedicineDetail response, int position){
//            binding.setItemEventHandler((PmReportViewModel) mEventHandler);
            binding.setModel(response);
//            if(holder.binding.layoutYw.getChildCount() != 0){
//                   return;
//            }
            for(MedicineC.MedicineDetail m : totalDates.get(position).getValue()){
                LRLayout mRl = content(context,m);
                holder.binding.layoutYw.addView(mRl);
            }

        }
    }
}
